package edu.utexas.mapreduce;

import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Notification {

	private String smtpHost = "localhost";
	private String username = null;
	private String password = null;
	
	Notification(String smtpHost) {
		this.smtpHost = smtpHost;
	}
	
	Notification(String smtpHost, String username, String password) {
		this.smtpHost = smtpHost;
		this.username = username;
		this.password = password;
	}
	
	
	/**
	 * @param from    - From: address
	 * @param to      - To: addresses - comma separated list
	 * @param subject - Subject:
	 * @param msg     - Message body 
	 */
	public void sendEmail(String from, 
	       	                  String to, String subject, String msg) {

	    Properties properties = System.getProperties();
	    // properties.setProperty("mail.smtp.host", smtpHost);
		properties.setProperty("mail.transport.protocol", "smtps");
		properties.setProperty("mail.smtps.host", smtpHost);
		properties.setProperty("mail.smtps.auth", "true");
		
		Session session = Session.getDefaultInstance(properties);

		try {
			MimeMessage message = new MimeMessage(session);

			// process the list of To: addresses
			ArrayList<String> recipientsArray = new ArrayList<String>();
			StringTokenizer st = new StringTokenizer(to, ",");
			while (st.hasMoreTokens()) {
				recipientsArray.add(st.nextToken());
			}

			int sizeTo = recipientsArray.size();
			InternetAddress[] addressTo = new InternetAddress[sizeTo];
			for (int i = 0; i < sizeTo; i++) {
				addressTo[i] = new InternetAddress(recipientsArray.get(i).toString());
			}		
			message.addRecipients(Message.RecipientType.TO, addressTo);
			
			// Set the remaining fields
			message.setFrom(new InternetAddress(from));
			message.setSubject(subject);
			message.setText(msg);

			// Send message
			Transport transport = session.getTransport();
			transport.connect(smtpHost, username, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();

		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * @param from    - From: address
	 * @param to      - To: addresses - comma separated list
	 * @param subject - Subject:
	 * @param msg     - Message body 
	 * @param file    - Email Attachment
	 */
	public void sendEmailAttach(String from, 
	                  String to, String subject, String msg, String file) {

		Properties properties = System.getProperties();
	    // properties.setProperty("mail.smtp.host", smtpHost);
		properties.setProperty("mail.transport.protocol", "smtps");
		properties.setProperty("mail.smtps.host", smtpHost);
		properties.setProperty("mail.smtps.auth", "true");
		Session session = Session.getDefaultInstance(properties);
		session.setDebug(true);
		try {
			MimeMessage message = new MimeMessage(session);
			
			// process the list of To: addresses
			ArrayList<String> recipientsArray = new ArrayList<String>();
			StringTokenizer st = new StringTokenizer(to, ",");
			while (st.hasMoreTokens()) {
				recipientsArray.add(st.nextToken());
			}

			int sizeTo = recipientsArray.size();
			InternetAddress[] addressTo = new InternetAddress[sizeTo];
			for (int i = 0; i < sizeTo; i++) {
				addressTo[i] = new InternetAddress(recipientsArray.get(i).toString());
			}		
			message.addRecipients(Message.RecipientType.TO, addressTo);

			// Set the remaining fields
			message.setFrom(new InternetAddress(from));
			message.setSubject(subject);

			//multi-part message - set message text
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(msg);
			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			// Set the attachment
			messageBodyPart   = new MimeBodyPart();
			String filename   = file;
			DataSource source = new FileDataSource(filename);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(filename);
			multipart.addBodyPart(messageBodyPart);
			
			message.setContent(multipart); // join both parts as one message
			
			// Send Message
			Transport transport = session.getTransport();
			transport.connect(smtpHost, username, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();

		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}	
}
